What is check-error?
The check-error npm package is a utility for checking errors and their properties. It is primarily used for testing error conditions in a consistent and readable manner. It provides functions to validate the type of an error, its message, and other properties.
What are check-error's main functionalities?
check error type
This feature allows you to check if an error is of a specific type, such as TypeError, using the isTypeError method.
const checkError = require('check-error');
const TypeError = checkError.TypeError;
try {
// some code that may throw
} catch (e) {
if (checkError.isTypeError(e)) {
// handle TypeError specifically
}
}
check error instance
This feature allows you to check if an error is an instance of a specific error class using the instanceOf method.
const checkError = require('check-error');
try {
// some code that may throw
} catch (e) {
if (checkError.instanceOf(e, CustomError)) {
// handle CustomError specifically
}
}
check error message
This feature allows you to check if an error contains a specific message using the hasMessage method.
const checkError = require('check-error');
try {
// some code that may throw
} catch (e) {
if (checkError.hasMessage(e, 'expected message')) {
// handle errors with a specific message
}
}
Other packages similar to check-error
chai
Chai is an assertion library that can be paired with any testing framework. It includes similar functionalities for checking errors through its assert, expect, and should interfaces. Chai provides a more extensive set of assertions for various use cases compared to check-error.
jest
Jest is a testing framework that includes its own set of assertion methods for checking errors. It offers a built-in expect function that can be used to test for specific error types and messages. Jest provides a more integrated testing solution with built-in mocking and spying capabilities in addition to error checking.
assert
The assert module is built into Node.js and provides a simple set of assertion tests that can be used to test invariants. It includes assert.throws and assert.doesNotThrow methods for checking errors. While it is less feature-rich than check-error, it does not require any additional dependencies.
check-error
Error comparison and information related utility for node and the browser.
What is Check-Error?
Check-Error is a module which you can use to retrieve an Error's information such as its message
or constructor
name and also to check whether two Errors are compatible based on their messages, constructors or even instances.
Installation
Node.js
check-error
is available on npm. To install it, type:
$ npm install check-error
Browsers
You can also use it within the browser; install via npm and use the check-error.js
file found within the download. For example:
<script src="./node_modules/check-error/check-error.js"></script>
Usage
The primary export of check-error
is an object which has the following methods:
compatibleInstance(err, errorLike)
- Checks if an error is compatible with another errorLike
object. If errorLike
is an error instance we do a strict comparison, otherwise we return false
by default, because instances of objects can only be compatible if they're both error instances.compatibleConstructor(err, errorLike)
- Checks if an error's constructor is compatible with another errorLike
object. If err
has the same constructor as errorLike
or if err
is an instance of errorLike
.compatibleMessage(err, errMatcher)
- Checks if an error message is compatible with an errMatcher
RegExp or String (we check if the message contains the String).getConstructorName(errorLike)
- Retrieves the name of a constructor, an error's constructor or errorLike
itself if it's not an error instance or constructor.getMessage(err)
- Retrieves the message of an error or err
itself if it's a String. If err
or err.message
is undefined we return an empty String.
var checkError = require('check-error');
.compatibleInstance(err, errorLike)
var checkError = require('check-error');
var funcThatThrows = function() { throw new TypeError('I am a TypeError') };
var caughtErr;
try {
funcThatThrows();
} catch(e) {
caughtErr = e;
}
var sameInstance = caughtErr;
checkError.compatibleInstance(caughtErr, sameInstance);
checkError.compatibleInstance(caughtErr, new TypeError('Another error'));
.compatibleConstructor(err, errorLike)
var checkError = require('check-error');
var funcThatThrows = function() { throw new TypeError('I am a TypeError') };
var caughtErr;
try {
funcThatThrows();
} catch(e) {
caughtErr = e;
}
checkError.compatibleConstructor(caughtErr, Error);
checkError.compatibleConstructor(caughtErr, TypeError);
checkError.compatibleConstructor(caughtErr, RangeError);
.compatibleMessage(err, errMatcher)
var checkError = require('check-error');
var funcThatThrows = function() { throw new TypeError('I am a TypeError') };
var caughtErr;
try {
funcThatThrows();
} catch(e) {
caughtErr = e;
}
var sameInstance = caughtErr;
checkError.compatibleMessage(caughtErr, /TypeError$/);
checkError.compatibleMessage(caughtErr, 'I am a');
checkError.compatibleMessage(caughtErr, /unicorn/);
checkError.compatibleMessage(caughtErr, 'I do not exist');
.getConstructorName(errorLike)
var checkError = require('check-error');
var funcThatThrows = function() { throw new TypeError('I am a TypeError') };
var caughtErr;
try {
funcThatThrows();
} catch(e) {
caughtErr = e;
}
var sameInstance = caughtErr;
checkError.getConstructorName(caughtErr)
.getMessage(err)
var checkError = require('check-error');
var funcThatThrows = function() { throw new TypeError('I am a TypeError') };
var caughtErr;
try {
funcThatThrows();
} catch(e) {
caughtErr = e;
}
var sameInstance = caughtErr;
checkError.getMessage(caughtErr)